/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.caelum.vraptor.casehd;

import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.view.Results;
import com.sun.xml.internal.ws.api.pipe.Tube;
import dao.TicketDao;
import dao.NotasTicketDao;
import java.sql.SQLException;
import java.util.List;
import model.NotasTicket;
import model.Ticket;
import model.UsuarioLogado;
import model.TicketAtual;

/**
 *
 * @author 03100286
 */
@Resource
public class TicketController {

    private final Result result;
    private final UsuarioLogado usuarioLogado;
    private final TicketAtual ticketAtual;

    public TicketController(Result result, UsuarioLogado usuarioLogado,
      TicketAtual ticketAtual) {
        this.result = result;
        this.usuarioLogado = usuarioLogado;
        this.ticketAtual = ticketAtual;
    }

    @Path("/ticket")
    public void index() throws SQLException {
        TicketDao ticketDao = new TicketDao();      
        List lista = ticketDao.lista();
        result.include("lista",lista);
        //result.include("linhas",lista.size());
    }

    @Path("/ticket/editar/voltar")
    public void voltar() throws SQLException {
        result.use(Results.logic()).redirectTo(TicketController.class).index();
    }



    @Path("/ticket/salvar")
    @Post
    public void salvar(Ticket ticket) {
        TicketDao ticketDao = new TicketDao();
        try {
            ticket.setId_usuario(usuarioLogado.getUser().getId());
            ticketDao.insere(ticket);
            ticketAtual.setId_ticket( ticket.getId_ticket() );
            result.use(Results.logic()).redirectTo(TicketController.class).editar( ticket.getId_ticket() );
            //result.include("msg",ticket.getAssunto());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Path("/ticket/salvar_nota")
    @Post
    public void salvar_nota(NotasTicket notasTicket) {
        NotasTicketDao notaDao = new NotasTicketDao();
        try {
            notasTicket.setId_usuario(usuarioLogado.getUser().getId());
            notasTicket.setId_ticket( ticketAtual.getId_ticket() );
            if (notasTicket.getStatus_salvar() == null || !notasTicket.getStatus_salvar().toLowerCase().equals("on"))
                notasTicket.setStatus(0);
            else
                notasTicket.setStatus(1);
            notaDao.insere(notasTicket);
            result.use(Results.logic()).redirectTo(TicketController.class).editar( notasTicket.getId_ticket() );
            //result.include("msg",ticket.getAssunto());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Path("/ticket/editar/{id}")
    public void editar(Integer id) throws SQLException {
        System.out.println(id);
        /* busca o ticket com o ID informado */
        ticketAtual.setId_ticket( id );
        TicketDao tktDao = new TicketDao();
        result.include("ticket", tktDao.buscaPorID(id));       
    }
}


